package com.sskj.fca.ui.activity;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.fca.R;
import com.sskj.fca.bean.BillRecordBean;
import com.sskj.fca.presenter.BillindRecordPresenter;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by QinKang on 2018/12/28.
 */
@Route(path = RConfig.APP_BILLIND_RECORD)//账单流水
public class BillindRecordActivity extends BaseActivity<BillindRecordPresenter> {

    @BindView(R.id.rl_choose_start_time)
    RelativeLayout rlChooseStartTime;
    @BindView(R.id.rl_choose_end_time)
    RelativeLayout rlChooseEndTime;
    @BindView(R.id.tv_bill_search)
    TextView tvBillSearch;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @Autowired
    String moneyType;
    private SlimAdapter slimAdapter;
    private MVCCoolHelper<List<BillRecordBean>> mvcHelper;
    private TimePickerView pvTime;
    private String startDate;
    private String endDate;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_billing_record;
    }

    @Override
    public BillindRecordPresenter getPresenter() {
        return new BillindRecordPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.app_activity_billing_record));
        ARouter.getInstance().inject(this);

        slimAdapter = SlimAdapter.create().register(R.layout.app_recy_item_bill_record, new SlimInjector<BillRecordBean>() {
            @Override
            public void onInject(BillRecordBean data, IViewInjector injector, List payloads) {
                if (data.getPrice().startsWith("-")) {
                    injector.textColor(R.id.tv_item_roll_change_money, ContextCompat.getColor(BillindRecordActivity.this, R.color.app_color));
                } else {
                    injector.textColor(R.id.tv_item_roll_change_money, ContextCompat.getColor(BillindRecordActivity.this, R.color.app_da7));

                }
                injector.text(R.id.tv_item_roll_type, data.getMemo())
                        .text(R.id.tv_item_roll_data, TimeFormatUtil.SF_FORMAT_F.format(new Date(Long.valueOf(Long.valueOf(data.getAddtime()) * 1000))))
                        .text(R.id.tv_item_roll_change_money, data.getPrice())
                        .text(R.id.tv_item_roll_end_money, data.getNprice());
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setAutoLoadMore(true);
        mvcHelper.setNeedCheckNetwork(false);
        mvcHelper.setAdapter(slimAdapter);

        String format = TimeFormatUtil.SF_FORMAT_B.format(new Date());
        refresh(format, format);

        ClickUtil.click(rlChooseStartTime, () -> {
            pvTime = new TimePickerBuilder(this, (date, v) -> {
                tvStartTime.setText(TimeFormatUtil.SF_FORMAT_B.format(date));
                startDate = TimeFormatUtil.SF_FORMAT_B.format(date);
            }).setType(new boolean[]{true, true, true, false, false, false}).build();// 默认全部显示
            pvTime.show();
        });
        ClickUtil.click(rlChooseEndTime, () -> {
            pvTime = new TimePickerBuilder(this, (date, v) -> {
                tvEndTime.setText(TimeFormatUtil.SF_FORMAT_B.format(date));
                endDate = TimeFormatUtil.SF_FORMAT_B.format(date);
            }).setType(new boolean[]{true, true, true, false, false, false}).build();// 默认全部显示
            pvTime.show();
        });
        ClickUtil.click(tvBillSearch, () -> {
            if (TextUtils.isEmpty(moneyType)) {
                return;
            }
            if (TextUtils.isEmpty(endDate) && TextUtils.isEmpty(startDate)) {
                ToastUtil.showShort("请选择搜索日期范围");
                return;
            }
            if (compare_date(startDate, endDate) != 1) {
                refresh(startDate, endDate);
            } else {
                ToastUtil.showShort("请注意日期先后顺序");
                return;
            }
        });

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(true)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
    }

    private void refresh(String start, String end) {
        ModelRx2DataSource<BillRecordBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<BillRecordBean>)
                        page -> mPresenter.getBillRecordList(moneyType.equals("JC") ? "pb" : "cny", start, end, page, 10), 10);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.refresh();
    }

    public static int compare_date(String DATE1, String DATE2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2后");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2前");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }


}
